package com.hcrazy.manchester.app.jdbc;

import java.util.ArrayList;
import java.util.List;

import com.hcrazy.manchester.app.pojo.Advert;
import com.hcrazy.manchester.app.pojo.PageModel;

public class AdvertDAO extends BaseDAO<Advert>{
	
	public Advert getAdvertById(String adId) throws Exception{
		String sql = "select * from him_Advert where adId = ?";
		return this.getJavaBean(sql, Advert.class,adId);
	}
	
	public Advert getAdvert(long moduleId,int moduleType) throws Exception {
		String sql = "select * from him_advert where moduleId = ? and moduleType = ? limit 1";
		
		return getJavaBean(sql, Advert.class, moduleId,moduleType);
	}
	
	public void addAdvert(Advert Advert) throws Exception{
		String sql = "insert into him_Advert (";
		sql += "type,adPos,moduleType,moduleId,adType,adContent,adDesc,status";
		sql += ") values (";
		sql += ":type,:adPos,:moduleType,:moduleId,:adType,:adContent,:adDesc,:status";
		sql += ")";
		this.update(sql, Advert);
	}
	
	public void updateAdvert(Advert Advert) throws Exception{
		String sql = "update him_Advert set ";
		sql += "type = :type,adPos=:adPos,moduleType=:moduleType,moduleId=:moduleId,adType=:adType,adContent=:adContent,adDesc=:adDesc,status=:status";
		sql += " where adId = :adId";
		
		this.update(sql, Advert);
	}
	
	public void deleteAdvert(int adId) throws Exception{
		String sql = "delete from him_Advert where adId = ?";
		this.commonUpdate(sql, adId);
	}
	
	public void deleteAdvert(long moduleId,int moduleType) throws Exception{
		String sql = "delete from him_Advert where moduleId = ? and moduleType = ?";
		this.commonUpdate(sql, moduleId,moduleType);
	}
	
	public PageModel getPagedAdverts(PageModel model) throws Exception{
		StringBuilder querySql = new StringBuilder("select * from him_Advert ");
		StringBuilder countSql = new StringBuilder("select count(*) from him_Advert");
		StringBuilder whereSql = new StringBuilder(" where AdvertLevel = ?");
		List<Object> paramList = new ArrayList<Object>();
		
		return this.getPageModel(model, querySql, countSql, whereSql, paramList, Advert.class);
	}
	
	public List<Advert> getAdvert(int type) throws Exception {
		String sql = "select * from him_advert where status = 1 and type = ?";
		
		return getList(sql,Advert.class,type);
	}
	
	public List<Advert> getAdvertList(int moduleType) throws Exception {
		String sql = "select * from him_advert where moduleType = ? and type = 0 and status = 1";
		
		return getList(sql,Advert.class,moduleType);
	}
	
	
}
